<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-23
 * Time: 17:00
 * 取送员抢单
 */

namespace app\api\service\store;

use app\api\model\StoreOrder;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;

class StaffOrderSer extends Token
{
    /**
     * 1.判断取送员信息
     * 2.判断订单和取送员是否是一个门店
     * 3.判断订单状态
     * 4.抢单成功 - 修改订单状态
     */

    // 用户信息
    private $user;

    // 取送员信息
    private $staff;

    // 订单信息
    private $order;

    function __construct($order_id)
    {
        $this->user = User::get(self::getCurrentUid());
        $this->order = StoreOrder::get($order_id);
    }

    // 入口方法
    public function entrance()
    {
        $this->staff = self::judgeStaff($this->user);
        return $this->judgeOrder();
    }

    // 判断订单状态
    public function judgeOrder()
    {
       if($this->order['status'] == 1) {
           $StoreOrder = new StoreOrder();
           $StoreOrder->save([
               'status' => 2,
               'pick_deliver_staff_id' => $this->staff['id'],
               'pick_deliver_staff_name' => $this->staff['name'],
               'pick_deliver_staff_phone' => $this->staff['phone'],
               'grab_time' => time()
           ],[ 'id' => $this->order['id'] ]);
           return writeJson(202, [], '抢单成功！');
       } else {
           throw new ParameterException([
               'msg' => [
                   'order' => '该订单已被别人抢',
               ]
           ]);
       }
    }
}